<?php

class Database
{
    
    private static $mysqli;
    private static $transactionOpen;
    
    
    public static function connect()
    {
        if (Database::$mysqli == null)
        {
            $mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_SCHEMA);
            Database::testErrorConnect();
            Database::$mysqli = $mysqli;
            Database::$transactionOpen = false;
        }
    }
    
    public static function disconnect()
    {
        if (Database::$mysqli != null)
        {
            Database::endTransaction();
            Database::$mysqli->close();
            Database::testError();
            Database::$mysqli = null;
        }
    }
    
    public static function query($sql, $params = null)
    {
        if (is_array($params) && (count($params) > 0))
            $sql = Database::setAllValues($sql, $params);
        Database::$mysqli->query($sql);
        Database::testError();
    }
    
    public static function queryOne($sql, $params = null)
    {
        if (is_array($params) && (count($params) > 0))
            $sql = Database::setAllValues($sql, $params);
        $resultSet = Database::$mysqli->query($sql);
        Database::testError();
        return $resultSet->fetch_array(MYSQL_ASSOC);
    }
    
    public static function queryMany($sql, $params = null)
    {
        if (is_array($params) && (count($params) > 0))
            $sql = Database::setAllValues($sql, $params);
        $resultSet = Database::$mysqli->query($sql);
        Database::testError();
        $rows = array();
        while ($rowData = $resultSet->fetch_array(MYSQL_ASSOC)) $rows[] = $rowData;
        return $rows;
    }

    public static function beginTransaction()
    {
        if (!Database::$transactionOpen)
        {
            Database::$mysqli->autocommit(false);
            Database::testError();
            Database::$transactionOpen = true;
        }
    }
    
    public static function commit()
    {
        Database::$mysqli->commit();
        Database::testError();
        Database::endTransaction();
    }
    
    public static function rollback()
    {
        Database::$mysqli->rollback();
        Database::testError();
        Database::endTransaction();
    }
    
    private static function endTransaction()
    {
        if (Database::$transactionOpen)
        {
            Database::$mysqli->autocommit(true);
            Database::testError();
            Database::$transactionOpen = false;
        }
    }
    
    public static function getLastInsertId()
    {
        return Database::$mysqli->insert_id;
    }
    
    private static function setValues(&$sql, $params, $quoted)
    {
        if ($quoted)
            list($markerStart, $markerEnd) = array('[', ']');
        else
            list($markerStart, $markerEnd) = array('{', '}');
        
        if (strpos($sql, $markerStart) !== false)
        {
            $sqlPieces = explode($markerEnd, $sql);
            $sql = "";
            foreach ($sqlPieces as $sqlPieceAndParam)
            {
                $x = explode($markerStart, $sqlPieceAndParam);
                $sql .= $x[0];
                if (count($x) > 1)
                {
                    if ($params[$x[1]] === null)
                        $sql .= 'NULL';
                    else if ($quoted)
                        $sql .= "'" . $params[$x[1]] . "'";
                    else
                        $sql .= $params[$x[1]];
                }
            }
        }
        
        return $sql;
    }
    
    private static function setAllValues($sql, $params)
    {
        $sql = Database::setValues($sql, $params, true);
        $sql = Database::setValues($sql, $params, false);
        return $sql;
    }
    
    private static function testError()
    {
        /*
        if (Database::$mysqli->error)
            die('MySQLi Error (' . Database::$mysqli->errno . ') ' . Database::$mysqli->error);
        */
    }
    
    private static function testErrorConnect()
    {
        if (Database::$mysqli->connect_error)
            die('MySQLi Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
    }
    
}

?>